import java.awt.event.ActionEvent;
import java.util.Vector;


public aspect ObserverProtocol {
	
	declare parents: ObserverImpl implements Observer;
	declare parents: DemoWithCommands implements Subject;	

	Subject Observer.subject = null;
	public void    ObserverImpl.update() {
		print(subject.getData());
	}
    public void     Observer.setSubject(Subject s) { subject = s; }
    public Subject  Observer.getSubject() { return subject; }
	pointcut stateChanges(Subject s):
        target(s) &&
        execution(public void DemoWithCommands.actionPerformed(..));
	
	public Object DemoWithCommands.getData() { return p.getBackground(); }

    after(Subject s): stateChanges(s) {
        for (int i = 0; i < s.getObservers().size(); i++) {
            ((Observer)s.getObservers().elementAt(i)).update();
        }
    }

    private Vector<Observer> Subject.observers = new Vector<Observer>();

    public void   Subject.removeObserver(Observer obs) {
        observers.removeElement(obs);
        obs.setSubject(null);
    }
    public void   Subject.addObserver(Observer obs) {
        observers.addElement( obs );
        obs.setSubject(this);
    }
    public Vector Subject.getObservers() { return observers; }

}